// 管理 Last Session 的类
function SessionManager() {
    // 用于表示Session Manager的常量
    var smToken = 'MaxthonSessionManager';

    // 成员变量
    this.length = 0; // 已保存的LastSession的个数
    this.list = []; // 保存LastSession的名字（以时间日期命名）
    this.sessions = {}; // 保存LastSessions
    var ls = window.localStorage; // 指向LocalStorage

    // 从 localStorage 中读取上次保存的SessionManager对象
    this.Load = function() {
        if (ls[smToken]) {
            var tempObject = JSON.parse(ls[smToken]);
            this.length = tempObject.length;
            this.list = tempObject.list;
            // 将Object重新初始化为Session对象
            for ( var it in tempObject.sessions) {
                var s = new Session(tempObject.sessions[it]);
                this.sessions[s.key] = s;
            }
        }
    };

    // 将 SessionManager 对象以JSON格式存入localStorage中
    this.Save = function() {
        ls[smToken] = JSON.stringify(this);
    };

    this.Clear = function() {
        if (ls[smToken]) {
            ls.removeItem(smToken);
        }
        this.Load();
    };

    // 获取当前 [上次未关闭页面]，返回一个Session对象
    this.GetLastSession = function() {
        lastObject = runtime.ConfigManager;
        dataList = lastObject.get("maxthon.config",
                "browser.unclosed.tabs.last");
        var s = new Session(this.GetCurrentTime(), dataList);
        return s;
    };

    this.AddSession = function(s) {
        // 确保要添加的Session不在列表中
        if (this.list.indexOf(s.key) == -1) {
            // 清除掉Session中已经打开过的链接
            s.ClearOpenedLinks();

            this.list.push(s.key);
            this.sessions[s.key] = s;
            this.length = this.list.length;
        } else {
            alert('该Session已经在列表中');
        }
    };

    // 将当前 [上次未关闭页面] 添加到SessionManager中
    this.AddLastSession = function() {
        var s = this.GetLastSession();
        this.AddSession(s);
    };

    this.GetSession = function(key) {
        var index = this.list.indexOf(key);
        if (index != -1) {
            return this.sessions[key];
        }
        return null;
    };

    this.DeleteSession = function(key) {
        var index = this.list.indexOf(key);
        if (index != -1) {
            this.list.splice(index, 1);
            delete (this.sessions[key]);
            this.length = this.list.length;
        }
    };

    this.LoadSession = function(str) {
        var s = new Session(JSON.parse(str));
        this.AddSession(s);
    };

    // 设置 [上次未关闭页面]
    this.SetLastSessionByKey = function(key) {
        var s = this.GetSession(key);
        if (s !== null) {
            var dataList = JSON.stringify(s.list);
            lastObject = runtime.ConfigManager;
            lastObject.set("maxthon.config", "browser.unclosed.tabs.last",
                    dataList);
            alert('设置成功');
        } else {
            alert('不能设置为一个不存在的Session');
        }
    };

    this.ClearCurrentSession = function() {
        var dummyStr = CONST.STR_DUMMY_LINK;
        lastObject = runtime.ConfigManager;
        lastObject
                .set("maxthon.config", "browser.unclosed.tabs.last", dummyStr);
        alert('清空成功');
    };

    /*
     * 返回Session Manager中是否有名为key的Session
     */
    this.HasSession = function(key) {
        return this.list.indexOf(key) != -1;
    };

    this.RenameSession = function(oldKey, newKey) {
        var s = this.GetSession(oldKey);
        // 效验参数
        if (s === null) {
            alert('Session: ' + oldKey + ' 不存在');
            return;
        }
        if (this.HasSession(newKey)) {
            alert('Session: ' + newKey + '已经存在，不能使用重复的名字');
            return;
        }
        // 重命名
        s.key = newKey;
        this.list[this.list.indexOf(oldKey)] = newKey;
        this.sessions[newKey] = s;
        delete (this.sessions[oldKey]);
    };

    // 获取并格式化当前时间，按照 2014-4-27 23:15:45 的格式
    this.GetCurrentTime = function() {
        var curTime = new Date();
        var strDate = curTime.getFullYear() + '-' + (curTime.getMonth() + 1)
                + '-' + curTime.getDate() + ' ' + curTime.getHours() + ':'
                + curTime.getMinutes() + ':' + curTime.getSeconds();
        return strDate;
    };

}
